package net.huashitong.supervision.task.controller;

import com.google.common.base.Strings;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysLoginFilter;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.filter.SysUserLoginFilter;
import com.sirdc.modules.sys.service.*;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.dataBean.TaskBackNewBean;
import net.huashitong.supervision.task.dataBean.TaskMainBean;
import net.huashitong.supervision.task.entity.*;
import net.huashitong.supervision.task.filter.*;
import net.huashitong.supervision.task.service.*;
import net.huashitong.synthetical.newSupervision.filter.TaskBackFilter;
import net.huashitong.user.entity.Postil;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/10
 * Time: 13:02
 * To change this template use File | Settings | File Templates.
 * Description:承办单位任务
 */
@Controller
@RequestMapping(value = "/taskUnit")
public class TaskUnitController extends JsonBaseController {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private TaskCategoryService categoryService;
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private SysLoginService sysLoginService;
    @Lazy
    @Autowired
    private TaskSubService taskSubService;
    @Autowired
    private TaskUserService taskUserService;

    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private TaskCategoryService taskCategoryService;
    @Autowired
    private TaskBackNewService taskBackNewService;
    @Autowired
    private HastenMessageService hastenMessageService;
    @Autowired
    private TaskLogNewService taskLogNewService;
    @Autowired
    private DeptTaskRanksService ranksService;

    @Override
    protected String getView(String s) {
        return "/modules/task/taskUnit/" + s;
    }

    public static String[] splitString(String s) {
        if (StringUtils.isNotBlank(s)) {
            return s.split(",");
        } else {
            return null;
        }
    }

    @RequestMapping(value = "/filter", method = RequestMethod.GET)
    public String filter() {
        return getView("filter");
    }

    /**
     * **************单位内部立项**********************
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "createTask")
    public String createTask(Model model) {
        String userId = SysUserUtils.getUserId();
        SysLogin login = sysLoginService.getById(userId);
        model.addAttribute("login", login);
        return getView("createTask");
    }

    @ResponseBody
    @RequestMapping(value = "/createTask", method = RequestMethod.POST)
    public Message createTask(TaskMainFilter filter) {
        if (StringUtils.isBlank(filter.getMainName())) {
            return coverMessage(UtilsTools.ERROR, "请填写任务名");
        }
        //如果不是周期性任务，需填写任务时限
        if ("1".equals(filter.getTimeType())) {
            if (StringUtils.isBlank(filter.getTimeLimit())) {
                return coverMessage(UtilsTools.ERROR, "请填写任务时限");
            }
        } else if ("2".equals(filter.getTimeType())) {
            if ("0".equals(filter.getPeriod())) {
                if (StringUtils.isBlank(filter.getDay())) {
                    return coverMessage(UtilsTools.ERROR, "请填写任务时限");
                }
            } else if ("1".equals(filter.getPeriod())) {
                if (StringUtils.isBlank(filter.getWeek())) {
                    return coverMessage(UtilsTools.ERROR, "请填写任务时限");
                }
            } else if ("2".equals(filter.getPeriod())) {
                if (StringUtils.isBlank(filter.getMonth())) {
                    return coverMessage(UtilsTools.ERROR, "请填写任务时限");
                }
            } else {
                if (StringUtils.isBlank(filter.getQuarter())) {
                    return coverMessage(UtilsTools.ERROR, "请填写任务时限");
                }
            }
        } else {
            if (StringUtils.isBlank(filter.getTimeLimits())) {
                return coverMessage(UtilsTools.ERROR, "请填写任务时限");
            }
        }
        String s = null;
        try {
            s = taskUnitService.createTask(filter);
        } catch (ParseException e) {
            e.printStackTrace();
            return coverMessage("500", e.getMessage());
        }
        if ("200".equals(s)) {
            return coverMessage("200", "保存成功");
        } else {
            return coverMessage("500", s);
        }
    }
    /****************** end *****************************
     */

    /**
     * 内部立项批转人员列表
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/endorseList", method = RequestMethod.POST)
    public Message endorseList(SysUser user) {
        String userId = "";
        if (StringUtils.isBlank(user.getSysId())) {
            userId = SysUserUtils.getUserId();
        } else {
            userId = user.getSysId();
        }
        user.setSysId(userId);
        List<SysUser> userList = sysLoginService.queryLowerList(user);
        return coverMessage("200", "success", userList);
    }


    /**
     * 单位待办的任务
     */
    @RequestMapping(value = "/noDoneUnit", method = RequestMethod.GET)
    public String noDoneUnit(Model model) {
        SysUser user = sysUserService.getById(SysUserUtils.getUserId());
        TaskMainFilter filter = new TaskMainFilter();
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        //单位督办任务
        List<TaskMain> overdueDeptMain = taskMainService.queryDBOverdueDept(filter);
        model.addAttribute("user", user);
        model.addAttribute("size", overdueDeptMain.size());
        return getView("noDoneUnit");
    }

    /**
     * 单位待办的任务
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/noDoneUnit", method = RequestMethod.POST)
    public JqGrid noDoneUnit(TaskUnitFilter filter) {
        List<TaskUnit> taskUnitList = new ArrayList<>();
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setTaskSubName(filter.getSearch());
            filter.setTaskMainName(filter.getSearch());
        }
        String roleId = SysUserUtils.getRoleId();
        //如果是单位主要领导管理员，查看单位所有的 任务
        if ("02".equals(roleId)) {
            String deptId = SysUserUtils.getDeptId();
            filter.setDeptId(deptId);
            filter.setStatus("2");
            filter.setState("0");
            taskUnitList = taskUnitService.getUnDoneUnit(filter);
            //查询当前
        } else {
            //只查看自己的任务
            filter.setUserId(SysUserUtils.getUserId());
            taskUnitList = taskUnitService.getUnDoneUser(filter);
        }
        return coverJqGrid(filter, taskUnitList);
    }

    /**
     * 内部待办(我创建的)
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dbNeibu", method = RequestMethod.POST)
    public JqGrid dbNeibu(TaskUnitFilter filter) {
        String userId = SysUserUtils.getUserId();
        String deptId = SysUserUtils.getDeptId();
        filter.setUserId(userId);
        filter.setDeptId(deptId);
        filter.setCategoryId("20191022100000000002");
        List<TaskUnit> unDoneUnit = taskUnitService.getUnDoneUnit(filter);
        return coverJqGrid(filter, unDoneUnit);
    }

    /**
     * 单位负责的待办单位重大事项
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/unDoneMain", method = RequestMethod.POST)
    public JqGrid unDoneMain(TaskMainFilter filter) {
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setMainName(filter.getSearch());
        }
        //单位督办任务
        List<TaskMain> overdueDeptMain = taskMainService.queryDBOverdueDept(filter);
        return coverJqGrid(filter, overdueDeptMain);
    }

    /**
     * 单位在办的任务
     */
    @RequestMapping(value = "/nowUnit", method = RequestMethod.GET)
    public String nowUnit(Model model) {
        SysUser user = sysUserService.getById(SysUserUtils.getUserId());
        TaskMainFilter filter = new TaskMainFilter();
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        //单位督办任务
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setMainName(filter.getSearch());
        }
        List<TaskMain> overdueDeptMain = taskMainService.queryZBOverdueDept(filter);
        model.addAttribute("user", user);
        model.addAttribute("size", overdueDeptMain.size());
        return getView("nowUnit");
    }

    /**
     * 单位在办的任务
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/nowUnit", method = RequestMethod.POST)
    public JqGrid nowUnit(TaskUnitFilter filter) {
        List<TaskUnit> taskUnitList = new ArrayList<>();
        String roleId = SysUserUtils.getRoleId();
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setTaskSubName(filter.getSearch());
            filter.setTaskMainName(filter.getSearch());
        }
        //如果是单位主要领导管理员，查看单位所有的 任务
        if ("02".equals(roleId)) {
            String deptId = SysUserUtils.getDeptId();
            filter.setDeptId(deptId);
            filter.setType("4");
            taskUnitList = taskUnitService.getNowUnit(filter);
        } else {
            //只查看自己的任务
            filter.setUserId(SysUserUtils.getUserId());
            taskUnitList = taskUnitService.getNowUser(filter);
        }
        return coverJqGrid(filter, taskUnitList);
    }

    /**
     * 单位负责的在办单位重大事项
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/nowDoneMain", method = RequestMethod.POST)
    public JqGrid nowDoneMain(TaskMainFilter filter) {
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setMainName(filter.getSearch());
        }
        //单位督办任务
        List<TaskMain> overdueDeptMain = taskMainService.queryZBOverdueDept(filter);
        return coverJqGrid(filter, overdueDeptMain);
    }

    /**
     * 单位完成的任务
     */
    @RequestMapping(value = "/doneUnit", method = RequestMethod.GET)
    public String doneUnit(Model model) {
        SysUser user = sysUserService.getById(SysUserUtils.getUserId());
        TaskMainFilter filter = new TaskMainFilter();
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setMainName(filter.getSearch());
        }
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        //单位督办任务
        List<TaskMain> overdueDeptMain = taskMainService.queryWCOverdueDept(filter);
        model.addAttribute("size", overdueDeptMain.size());
        model.addAttribute("user", user);
        return getView("doneUnit");
    }

    /**
     * 单位完成的任务
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doneUnit", method = RequestMethod.POST)
    public JqGrid doneUnit(TaskUnitFilter filter) {
        List<TaskUnit> taskUnitList = new ArrayList<>();
        String roleId = SysUserUtils.getRoleId();
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setTaskSubName(filter.getSearch());
            filter.setTaskMainName(filter.getSearch());
        }
        //如果是单位主要领导管理员，查看单位所有的 任务
        if ("02".equals(roleId)) {
            String deptId = SysUserUtils.getDeptId();
            filter.setDeptId(deptId);
            filter.setType("4");
            taskUnitList = taskUnitService.queryFilter(filter);
        } else {
            //只查看自己的任务
            filter.setUserId(SysUserUtils.getUserId());
            taskUnitList = taskUnitService.getDoneUser(filter);
        }
        return coverJqGrid(filter, taskUnitList);
    }

    /**
     * 单位负责的已完成单位重大事项
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doneMain", method = RequestMethod.POST)
    public JqGrid doneMain(TaskMainFilter filter) {
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setMainName(filter.getSearch());
        }
        //单位督办任务
        List<TaskMain> overdueDeptMain = taskMainService.queryWCOverdueDept(filter);
        return coverJqGrid(filter, overdueDeptMain);
    }

    /**
     * 单位全部的任务
     */
    @RequestMapping(value = "/allUnit", method = RequestMethod.GET)
    public String allUnit(Model model) {
        SysUser user = sysUserService.getById(SysUserUtils.getUserId());
        TaskMainFilter filter = new TaskMainFilter();
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setMainName(filter.getSearch());
        }
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        //单位督办任务
        List<TaskMain> overdueDeptMain = taskMainService.queryOverdueDept(filter);
        model.addAttribute("size", overdueDeptMain.size());
        model.addAttribute("user", user);
        return getView("allUnit");
    }

    /**
     * 单位qunabu的任务
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allUnit", method = RequestMethod.POST)
    public JqGrid allUnit(TaskUnitFilter filter) {
        List<TaskUnit> taskUnitList = new ArrayList<>();
        String roleId = SysUserUtils.getRoleId();
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setTaskSubName(filter.getSearch());
            filter.setTaskMainName(filter.getSearch());
        }
        //如果是单位主要领导管理员，查看单位所有的 任务
        if ("02".equals(roleId)) {
            String deptId = SysUserUtils.getDeptId();
            filter.setDeptId(deptId);
            taskUnitList = taskUnitService.queryFilter(filter);
        } else {
            //只查看自己的任务
            filter.setUserId(SysUserUtils.getUserId());
            taskUnitList = taskUnitService.getAllUser(filter);
        }
        return coverJqGrid(filter, taskUnitList);
    }

    /**
     * 单位负责的quanbu单位重大事项
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allDoneMain", method = RequestMethod.POST)
    public JqGrid allDoneMain(TaskMainFilter filter) {
        String deptId = SysUserUtils.getDeptId();
        filter.setOverdueDept(deptId);
        if (StringUtils.isNotBlank(filter.getSearch())) {
            filter.setMainName(filter.getSearch());
        }
        //单位督办任务
        List<TaskMain> overdueDeptMain = taskMainService.queryOverdueDept(filter);
        return coverJqGrid(filter, overdueDeptMain);
    }

    /**
     * 查看单位任务详情
     *
     * @param taskUnitId
     * @return
     */
    @RequestMapping(value = "/taskUnitDetail/{taskUnitId}", method = RequestMethod.GET)
    public String taskUnitDetail(Model model, @PathVariable String taskUnitId) {

        String userId = SysUserUtils.getUserId();
        String deptId = SysUserUtils.getDeptId();
        TaskUnit taskUnit = taskUnitService.getById(taskUnitId);
        if (ObjectUtils.isBlank(taskUnit)) {
            //再去人员任务去拿
            TaskUser byId = taskUserService.getById(taskUnitId);
            if (ObjectUtils.isNotBlank(byId)) {
                taskUnit = taskUnitService.getById(byId.getTaskUnitId());
            } else {
                return getView("detail");
            }
        }
        //如果当前任务是待签收，查看时将任务更新为 待反馈   /任务状态(0.待签收,1待反馈,2已反馈,3退回,4完成 。5已签收,6回退单-未签收，7回退单-待反馈，8回退单-已反馈，9回退单-完成)
        String taskType = taskUnit.getType();

        if (("0".equals(taskType) || "3".equals(taskType)) && (taskUnit.getDeptId().equals(deptId))) {
            if ("0".equals(taskType)) {

                taskUnit.setType("1");
            } else {
                taskUnit.setType("7");
            }
            taskUnit.setUserId(userId);
            taskUnitService.update(taskUnit);
        }

        //已反馈单位
        StringBuffer feedBacks = new StringBuffer();
        //未反馈单位
        StringBuffer noFeedBacks = new StringBuffer();

        TaskBackNewFilter filter = new TaskBackNewFilter();
        filter.setTaskSubId(taskUnit.getTaskSubId());
        List<TaskBackNew> allBackNewList = taskBackNewService.queryAll(filter);
        String taskMainId = taskUnit.getTaskMainId();
        //牵头单位名称集合
        String leadDepts = "";
        List<TaskUnit> leadDeptList = taskUnitService.getBySubIdAndUnitType(taskUnit.getTaskSubId(), "1", taskUnit.getTimeRound());
        for (TaskUnit unit : leadDeptList) {
            boolean flag = false;
            for (TaskBackNew backNew : allBackNewList) {
                if (unit.getDeptName().equals(backNew.getSenderName())) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                if (feedBacks.length() > 0) {
                    feedBacks.append("、" + unit.getDeptName());
                } else {
                    feedBacks.append(unit.getDeptName());
                }
            } else {
                if (noFeedBacks.length() > 0) {
                    noFeedBacks.append("、" + unit.getDeptName());
                } else {
                    noFeedBacks.append(unit.getDeptName());
                }
            }
            leadDepts += (unit.getDeptName() + " ");
        }
        //协办单位名称集合
        String xbDepts = "";
        List<TaskUnit> xbDeptList = taskUnitService.getBySubIdAndUnitType(taskUnit.getTaskSubId(), "2", taskUnit.getTimeRound());
        for (TaskUnit unit : xbDeptList) {
            boolean flag = false;
            for (TaskBackNew backNew : allBackNewList) {
                if (unit.getDeptName().equals(backNew.getSenderName())) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                if (feedBacks.length() > 0) {
                    feedBacks.append("、" + unit.getDeptName());
                } else {
                    feedBacks.append(unit.getDeptName());
                }
            } else {
                if (noFeedBacks.length() > 0) {
                    noFeedBacks.append("、" + unit.getDeptName());
                } else {
                    noFeedBacks.append(unit.getDeptName());
                }
            }
            xbDepts += (unit.getDeptName() + " ");
        }
        TaskMain taskMain = taskMainService.getById(taskMainId);
        TaskMainBean taskMainBean = new TaskMainBean();
        if (ObjectUtils.isNotBlank(taskMain.getOverdueDept())) {
            //本轮次的评价
            DeptTaskRanksEntity ownRank = ranksService.getOwnRank(taskMainId, taskUnit.getTimeRound(), taskUnit.getDeptId());
            model.addAttribute("taskRank", ownRank);
        }


        BeanUtils.copyProperties(taskMain, taskMainBean);
        String[] files = splitString(taskMain.getFile());
        String[] fileNames = splitString(taskMain.getFileName());
        String[] gJFiles = splitString(taskMain.getgJFile());
        String[] gJfileName = splitString(taskMain.getgJFileName());
        String[] resultFiles = splitString(taskMain.getMainResultFile());
        String[] resultFileNames = splitString(taskMain.getMainResultFileName());
        List<Map<String, Object>> fileMapList = new ArrayList<>();
        if (ObjectUtils.isNotBlank(files)) {
            for (int i = 0; i < files.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("file", files[i]);
                map.put("fileName", fileNames[i]);
                fileMapList.add(map);
            }
            taskMainBean.setFiles(fileMapList);
        }
        if (ObjectUtils.isNotBlank(gJFiles)) {
            List<Map<String, Object>> gjFileMapList = new ArrayList<>();
            for (int i = 0; i < gJFiles.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("file", gJFiles[i]);
                map.put("fileName", gJfileName[i]);
                gjFileMapList.add(map);
            }
            taskMainBean.setgJFile(gjFileMapList);
        }
        if (ObjectUtils.isNotBlank(resultFiles)) {
            List<Map<String, Object>> resultFileMapList = new ArrayList<>();
            for (int i = 0; i < resultFiles.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("file", resultFiles[i]);
                map.put("fileName", resultFileNames[i]);
                resultFileMapList.add(map);
            }
            taskMainBean.setMainResultFile(resultFileMapList);
        }

    /*     List<TaskUser> taskUserList = taskUserService.getByTaskUnitId(taskUnitId);
       List<Map> list = new ArrayList<>();
        for (TaskUser taskUser : taskUserList) {
            SysUser sysUser = sysUserService.getById(taskUser.getUnitUserId());
            Map<String, Object> map = new HashMap<>();
            map.put("sysUser", sysUser);
            map.put("taskUser", taskUser);
            list.add(map);
        }*/
        String userCreate = "";
        if ("03".equals(SysUserUtils.getRoleId()) || "04".equals(SysUserUtils.getRoleId())) {
            TaskUserFilter taskUserFilter = new TaskUserFilter();
            taskUserFilter.setTaskUnitId(taskUnitId);
            taskUserFilter.setUnitUserId(SysUserUtils.getUserId());
            List<TaskUser> taskUsers = taskUserService.queryFilter(taskUserFilter);
            if (ObjectUtils.isNotBlank(taskUsers)) {
                for (TaskUser taskUser : taskUsers) {
                    //如果登录用户是分管或者经办人 ，判断当前人员任务状态
                    if ("0".equals(taskUser.getStatus())) {
                        //待签收时查看的 时候将状态改为已签收5
                        taskUser.setStatus("5");
                        taskUserService.update(taskUser);
                    }
                }
                userCreate = taskUsers.get(0).getCreateUser();
            }
        }
        //返回一个登陆信息用于前端判断当前用户角色展示不同界面
        SysLogin sysLogin = sysLoginService.getById(userId);
        //当前单位任务的所有反馈
        List<TaskBackNew> taskBackNews = taskBackNewService.getByTaskUnitId(taskUnitId);
        //任务创建人id
        String mainCreateUser = taskMain.getCreateUser();
        model.addAttribute("userCreate", userCreate);
        //当前登录人的反馈信息
        List<TaskBackNew> myBackList = taskBackNewService.getByTaskUnitIdAndSenderId(taskUnitId,taskUnit.getUserId() );
        List<TaskBackNewBean> myBackBeanList = new ArrayList<>();
        for (TaskBackNew taskBackNew : myBackList) {
            TaskBackNewBean taskBackNewBean = new TaskBackNewBean();
            BeanUtils.copyProperties(taskBackNew, taskBackNewBean);
            String[] file = splitString(taskBackNew.getFile());
            String[] fileName = splitString(taskBackNew.getFileName());
            List<Map<String, Object>> backFileList = new ArrayList<>();
            if (ObjectUtils.isNotBlank(file)) {
                for (int i = 0; i < file.length; i++) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("file", file[i]);
                    map.put("fileName", fileName[i]);
                    backFileList.add(map);
                }
            }

            taskBackNewBean.setFiles(backFileList);
            myBackBeanList.add(taskBackNewBean);
        }
        //反馈给我的信息
        List<TaskBackNew> giveMeBackList = taskBackNewService.getByTaskUnitIdAndRecipientId(taskUnitId, userId);
        List<TaskBackNewBean> giveMeBackBeanList = new ArrayList<>();

        for (TaskBackNew taskBackNew : giveMeBackList) {
            TaskBackNewBean taskBackNewBean = new TaskBackNewBean();

            BeanUtils.copyProperties(taskBackNew, taskBackNewBean);
            String[] file = splitString(taskBackNew.getFile());
            String[] fileName = splitString(taskBackNew.getFileName());
            List<Map<String, Object>> backFileList = new ArrayList<>();
            if (ObjectUtils.isNotBlank(file)) {
                for (int i = 0; i < file.length; i++) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("file", file[i]);
                    map.put("fileName", fileName[i]);
                    backFileList.add(map);
                }
            }
            taskBackNewBean.setFiles(backFileList);
            giveMeBackBeanList.add(taskBackNewBean);
        }
        //协办反馈给单位的
        List<TaskBackNew> giveDeptBackList = taskBackNewService.getByTaskUnitIdAndReceiveDeptAndSubId(taskUnitId, deptId, taskUnit.getTaskSubId());
        List<TaskBackNewBean> giveDeptBackBeanList = new ArrayList<>();
        for (TaskBackNew taskBackNew : giveDeptBackList) {
            TaskBackNewBean taskBackNewBean = new TaskBackNewBean();

            BeanUtils.copyProperties(taskBackNew, taskBackNewBean);
            String[] file = splitString(taskBackNew.getFile());
            String[] fileName = splitString(taskBackNew.getFileName());
            List<Map<String, Object>> backFileList = new ArrayList<>();
            if (ObjectUtils.isNotBlank(file)) {
                for (int i = 0; i < file.length; i++) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("file", file[i]);
                    map.put("fileName", fileName[i]);
                    backFileList.add(map);
                }
            }
            taskBackNewBean.setFiles(backFileList);
            giveDeptBackBeanList.add(taskBackNewBean);
        }
        //当前人员批转给下级的任务
        List<TaskUser> taskUserList = taskUserService.getByCreateUserAndTaskUnitId(SysUserUtils.getUserId(), taskUnitId);
//流程记录
        List<TaskLogNew> logNewList = taskLogNewService.getByUnitId(taskUnitId);

        //历史轮次
        List<Map<String, Object>> oldTask = taskUnitService.getByOldTaskUnit(taskUnit.getTaskSubId(), deptId);
        model.addAttribute("oldTask", oldTask);
        model.addAttribute("taskUserList", taskUserList);
        model.addAttribute("myBackList", myBackBeanList);
        model.addAttribute("giveDeptBackList", giveDeptBackBeanList);
        model.addAttribute("giveMeBackList", giveMeBackBeanList);
        model.addAttribute("mainCreateUser", mainCreateUser);
        model.addAttribute("sysLogin", sysLogin);
        model.addAttribute("taskBackNews", taskBackNews);
//        model.addAttribute("list", list);
        model.addAttribute("taskMain", taskMainBean);
        model.addAttribute("taskUnit", taskUnit);
        model.addAttribute("leadDepts", leadDepts);
        model.addAttribute("xbDepts", xbDepts);
        model.addAttribute("xbDeptList", xbDeptList);
        model.addAttribute("leadDeptList", leadDeptList);
        model.addAttribute("logNewList", logNewList);
        model.addAttribute("feedBacks", feedBacks.toString());
        model.addAttribute("noFeedBacks", noFeedBacks.toString());
        return getView("detail");
    }

    /**
     * 更新当前批转下去的任务（将当前人员任务失效，生效）
     *
     * @param taskUserId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateTaskUser")
    public Message updateTaskUser(String taskUserId) {
        String result = taskUserService.updateTaskUser(taskUserId);
        if ("200".equals(result)) {
            return coverMessage("200");
        } else {
            return coverMessage("500");
        }
    }

    /**
     * 查询当前单位任务的历史轮次记录
     *
     * @param taskSubId
     * @param deptId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/oldTask", method = RequestMethod.POST)
    public List<Map<String, Object>> oldTaskUnit(String taskSubId, String deptId) {
        //根据当前任务查询历史任务
        List<Map<String, Object>> oldTask = taskUnitService.getByOldTaskUnit(taskSubId, deptId);
        return oldTask;
    }


    /**
     * 批转人员列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/endorseList/{taskUnitId}", method = RequestMethod.GET)
    public List<SysUser> endorseList(@PathVariable String taskUnitId) {
        List<SysUser> userList = taskUnitService.queryEndorseList(taskUnitId, null);
        return userList;
    }

    /**
     * 保存批转人员任务
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveTaskUser", method = RequestMethod.POST)
    public Message saveTaskUser(@ModelAttribute TaskUnitFilter filter) {
        String s = taskUnitService.saveTaskUser(filter);
        if ("200".equals(s)) {
            return coverMessage("200", "保存成功");
        } else {
            return coverMessage("500", "未添加经办人");
        }
    }

    @ResponseBody
    @RequestMapping("/test")
    public void test(String s) {
        TaskUnit taskUnit = taskUnitService.getById(s);
        TaskUnit unit = new TaskUnit();
        BeanUtils.copyProperties(taskUnit, unit);
        String createUser = unit.getCreateUser();
        taskUnitService.saveUnit(unit);
        System.out.println(createUser);
    }


    @ResponseBody
    @RequestMapping(value = "/backUserList", method = RequestMethod.GET)
    public List<Map> backUserList(String taskUnitId) {
        return taskUnitService.getBackUserList(taskUnitId);

    }

    /**
     * 保存反馈信息
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/taskBackNew", method = RequestMethod.POST)
    public Message backTask(@ModelAttribute TaskBackNewFilter filter) {
        String s = taskBackNewService.saveBack(filter);
        if ("200".equals(s)) {
            return coverMessage("200", "反馈成功");
        } else if ("500".equals(s)) {
            return coverMessage("500", "反馈失败");
        } else {
            return coverMessage("500", s);
        }
    }
    //

    /**
     * 攻坚任务 督查组反馈给作战室
     *
     */
    @ResponseBody
    @RequestMapping(value = "/gongjianback", method = RequestMethod.POST)
    public Message gongjianback(@ModelAttribute TaskBackNewFilter filter) {

        String taskUnitId = filter.getTaskUnitId();
        String taskMainId = filter.getTaskMainId();
        if(StringUtils.isBlank(taskUnitId)||StringUtils.isBlank(taskMainId))
        {
            return coverMessage("500","参数错误");
        }
        String deptId = filter.getDeptId();
        if(StringUtils.isBlank(deptId))
        {
            filter.setDeptId(SysUserUtils.getDeptId());
        }
        if(StringUtils.isBlank(filter.getLoginerId()))
        {
            filter.setLoginerId(SysUserUtils.getUserId());
        }

        String s = taskBackNewService.saveGongJianTaskBack(filter);

        if ("200".equals(s)) {
            return coverMessage("200", "反馈成功");
        } else if ("500".equals(s)) {
            return coverMessage("500", "反馈失败");
        } else {
            return coverMessage("500", s);
        }
    }


    /**
     * 单位重大任务 督查单位汇总主任务结果反馈至督查组(旧 )
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveMainResult", method = RequestMethod.POST)
    public Message saveMainResult(TaskMainFilter filter) {
        TaskMain taskMain = taskMainService.getById(filter.getSysId());
        if (StringUtils.isBlank(filter.getMainResultFile())) {
            return coverMessage("500", "请上传汇总附件");
        }
        //将汇总的结果更新
        taskMain.setMainResult(filter.getMainResult());
        taskMain.setMainResultFile(filter.getMainResultFile());
        taskMain.setMainResultFileName(filter.getMainResultFileName());
        //设置主任务任务为待办任务；yifankui
        taskMain.setStatus("3");
        taskMain.setOverdueDeptState(null);
        taskMainService.update(taskMain);
        return coverMessage("200", "反馈成功");
    }




    /**
     * 单位重大任务 督查单位汇总主任务结果反馈至督查组(新 )
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveDuBanBackInfo", method = RequestMethod.POST)
    public Message saveDuBanBackInfo(TaskMainFilter filter) {
       /* TaskMain taskMain = taskMainService.getById(filter.getSysId());
        if (StringUtils.isBlank(filter.getMainResultFile())) {
            return coverMessage("500", "请上传汇总附件");
        }*/

        TaskBackNewFilter backfilter = new TaskBackNewFilter();
        backfilter.setContent(filter.getMainResult());
        backfilter.setTaskMainId(filter.getSysId());
        backfilter.setFile(filter.getMainResultFile());
        backfilter.setFileName(filter.getMainResultFileName());
        String code = taskBackNewService.saveDuBanBackInfo(backfilter);
        //将汇总的结果更新
       /* taskMain.setMainResult(filter.getMainResult());
        taskMain.setMainResultFile(filter.getMainResultFile());
        taskMain.setMainResultFileName(filter.getMainResultFileName());
        //设置主任务任务为待办任务；yifankui
        taskMain.setStatus("3");
        taskMain.setOverdueDeptState(null);
        taskMainService.update(taskMain);*/
        return coverMessage(code, "反馈成功");
    }

    /**
     * 反馈状态判断
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public Message updateStatus(String taskBackId, String status) {
        String s = taskBackNewService.updateStatus(taskBackId, status);
        if ("200".equals(s)) {
            return coverMessage("200", "办理成功");
        } else {
            return coverMessage("500", "办理失败");
        }
    }


    /**
     * 单位角色查看催办过的信息
     *
     * @return
     */
    @RequestMapping(value = "/hastenMessage", method = RequestMethod.GET)
    public String showHasten() {
        return getView("hasten");
    }

    @ResponseBody
    @RequestMapping(value = "/showHasten", method = RequestMethod.POST)
    public JqGrid showHasten(HastenMessageFilter filter) {
        filter.setUserId(SysUserUtils.getUserId());
        List<HastenMessage> hastenMessageList = hastenMessageService.queryFilter(filter);
        return coverJqGrid(filter, hastenMessageList);
    }

    /**
     * 单位人员查看催办信息，信息状态改为已读
     *
     * @param hastenId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateHasten/{hastenId}", method = RequestMethod.GET)
    public HastenMessage updateHasten(@PathVariable String hastenId) {
        HastenMessage hastenMessage = hastenMessageService.getById(hastenId);
        hastenMessage.setStatus("1");
        hastenMessageService.update(hastenMessage);
        return hastenMessage;
    }

    /**
     * 主办打开协办单位的任务页面
     *
     * @param taskUnitId
     * @return
     */
    @RequestMapping(value = "/showXbTask/{taskUnitId}", method = RequestMethod.GET)
    public String showXbTask(Model model, @PathVariable String taskUnitId) {
        TaskUnit taskUnit = taskUnitService.getById(taskUnitId);
        //当前单位任务的所有反馈
        List<TaskBackNew> taskBackNews = taskBackNewService.getByTaskUnitId(taskUnitId);
        List<TaskLogNew> logNewList = taskLogNewService.getByUnitId(taskUnitId);
        model.addAttribute("taskUnit", taskUnit);
        model.addAttribute("taskBackNews", taskBackNews);
        model.addAttribute("logNewList", logNewList);
        return getView("showXbTask");
    }

}
